package com.cskaoyan.converter;

import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author stone
 * @date 2022/10/13 23:05
 */
@Component
public class String2DateConverter implements Converter<String, Date> {
    @SneakyThrows
    @Override
    public Date convert(String source) {
        SimpleDateFormat simpleDateFormat = null;
        if (source.length() == 19) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }else {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        }
        Date date = simpleDateFormat.parse(source);
        return date;
    }
}
